<?php

namespace app\admin\controller;

use app\admin\logic\system\SystemDeptLogic;
use app\admin\logic\system\SystemPostLogic;
use app\admin\logic\system\SystemUserLogic;
use app\common\service\SignService;
use support\Request;

class SyncController extends BaseController
{
    /**
     * 不需要登录的方法
     */
    protected array $noNeedLogin = ['syncInfo'];

    public function syncInfo(Request $request)
    {
        if (!$request->isPost()) {
            return $this->fail('请求方式错误');
        }

        $params = [
            'timestamp' => $request->post('timestamp'),
            'nonce'     => $request->post('nonce'),
            'sign'      => $request->post('sign'),
            'data'      => json_decode($request->post('data'), true),
        ];

        if (!SignService::verify($params)) {
            return $this->fail('签名错误');
        }
        $data = $params['data'];
        if (empty($data['action']) || !in_array($data['action'], ['sync_user', 'sync_dept', 'sync_post'])) {
            return $this->fail('参数错误');
        }
        if (empty($data['info'])) {
            return $this->fail('参数错误');
        }

        switch ($data['action']) {
            case 'sync_user':
                $logic = new SystemUserLogic();
                break;
            case 'sync_dept':
                //$logic = new SystemDeptLogic();
                break;
            case 'sync_post':
                $logic = new SystemPostLogic();
                break;
        }
        if (!empty($logic)) {
            return $logic->syncInfo($data['info']);
        }

        return $this->fail('同步失败');
    }
}